package
{
	import levelloader.TileData;
	
	public class DynamicLayer extends GameLayer
	{
		private var isMovable:Boolean;
		
		public function DynamicLayer(world:World, layerID:uint, parallaxRatio:Number, movable:Boolean = true)
		{
			isMovable = movable;
			super(world, layerID, parallaxRatio);						
		}

		public override function onEnterFrame(dt:Number):void
		{
			var camX:Number = _parent.camera.getCameraPosX(_parallaxRatio);
			var camY:Number = _parent.camera.getCameraPosY(_parallaxRatio);
			
			var currentLeftIndex:uint = Math.floor(camX/tileWidth);
			var currentRightIndex:uint = Math.ceil((camX+camWidth-1)/tileWidth);
			
			if(currentLeftIndex > leftColumnIndex)
			{
				leftColumnIndex = currentLeftIndex;
				
				var dropPool:Vector.<tinku> = new Vector.<tinku>();
				var child:tinku;
				var numChild:int = this.numChildren;
				trace("num active enemies: " + numChild);
				//check if objects in view are still visible after the last 
				//camera update. If not, then remove these from the display tree.
				for(var id:int = 0; id < numChild; ++id)
				{							
					child = this.getChildAt(id) as tinku;
					
					if(child != null)
					{
						if(!child.intersectsLine(camX, camX+camWidth-1))
						{
							//Don't delete the elements while iterating the collection. 
							//Mark the objects for deletion by adding them to a temp pool.
							dropPool.push(child);
						}		
						else
						{
							child.onEnterFrame(dt);
						}
					}					
				}	
				//iterate over the temp pool and delete all the objects
				var numDropObjs:uint = dropPool.length;			
				for (var dID:uint = 0; dID < numDropObjs; ++dID)
				{
					dropPool[dID].removeFromParent(true);
				}				
			}
			
			//iterate through the general layer data and 
			//add new objects that are visible from the camera 
			//to this layer's display tree.
			if(currentRightIndex < mapWidth && currentRightIndex > rightColumnIndex)
			{
				rightColumnIndex = currentRightIndex;
				
				var dataColumn:Vector.<TileData> = layer.getData()[rightColumnIndex];
				renderColumn(dataColumn);
			}
			
			//transform the layer from world space to view space.
			this.x = -camX;
			this.y = -camY;			
		}
		
		protected override function renderColumn(dataColumn:Vector.<TileData>):void
		{
			var columnLength:uint = dataColumn.length;
			for (var columnIdx:int = 0; columnIdx < columnLength; ++columnIdx)
			{
				var name:String = _parent.getTMXTileMap().tileMapper[dataColumn[columnIdx].textureID];
				
				var isCollectable:Boolean = false;
				if(_layerID == Assets.COLLECTABLES)
					isCollectable = true;
								
				var img:GameObject;
				if(isMovable)
					img = new tinku(_parent, dataColumn[columnIdx].textureID);
				else
				{
					if(isCollectable)
						img = new Collectable(_parent, dataColumn[columnIdx].textureID);
					else
						img = new WorldObject(_parent, dataColumn[columnIdx].textureID);
				}
					
				
				img.x = dataColumn[columnIdx].tileIndexX * tileWidth;
				img.pivotY = img.height;
				img.y = dataColumn[columnIdx].tileIndexY * tileWidth + tileWidth;					
				addChild(img);						
			}								
		}		
	}
}